package com.flyco.modules.admissionregistration.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.util.IPUtils;
import com.flyco.common.util.address.AddressUtil;
import com.flyco.common.util.address.WSSsdrAddress;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.admissionregistration.entity.ReceptionUser;
import com.flyco.modules.admissionregistration.service.IReceptionUserService;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderAddition;
import com.flyco.modules.oms.entity.OmsOrderBatch;
import com.flyco.modules.oms.entity.OmsOrderItem;
import com.flyco.modules.oms.model.OmsOrderImportModel;
import com.flyco.modules.oms.model.OmsOrderItemModel;
import com.flyco.modules.oms.vo.OmsOrderImportPage;
import com.flyco.modules.pms.entity.PmsProductPriceRegulate;
import com.flyco.modules.pms.vo.PmsProductSkuForOrderVO;
import com.flyco.modules.ums.entity.UmsMember;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.entity.ImportParams;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 接待人
 * @Author: flyco
 * @Date:   2023-06-09
 * @Version: V1.0
 */
@Slf4j
@Api(tags="接待人")
@RestController
@RequestMapping("/f/receptionuser/receptionUser")
public class ReceptionUserController extends BaseController<ReceptionUser, IReceptionUserService> {
	@Autowired
	private IReceptionUserService receptionUserService;
	
	/**
	 * 分页列表查询
	 *
	 * @param receptionUser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "接待人-分页列表查询")
	@ApiOperation(value="接待人-分页列表查询", notes="接待人-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReceptionUser receptionUser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ReceptionUser> queryWrapper = QueryGenerator.initQueryWrapper(receptionUser, req.getParameterMap());
		queryWrapper.eq("status",1);
		Page<ReceptionUser> page = new Page<ReceptionUser>(pageNo, pageSize);
		IPage<ReceptionUser> pageList = receptionUserService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param receptionUser
	 * @return
	 */
	@AutoLog(value = "接待人-添加")
	@ApiOperation(value="接待人-添加", notes="接待人-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ReceptionUser receptionUser) {
		receptionUserService.save(receptionUser);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param receptionUser
	 * @return
	 */
	@AutoLog(value = "接待人-编辑")
	@ApiOperation(value="接待人-编辑", notes="接待人-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ReceptionUser receptionUser) {
		receptionUserService.updateById(receptionUser);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "接待人-通过id删除")
	@ApiOperation(value="接待人-通过id删除", notes="接待人-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		receptionUserService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "接待人-批量删除")
	@ApiOperation(value="接待人-批量删除", notes="接待人-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.receptionUserService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "接待人-通过id查询")
	@ApiOperation(value="接待人-通过id查询", notes="接待人-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ReceptionUser receptionUser = receptionUserService.getById(id);
		return Result.ok(receptionUser);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param receptionUser
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, ReceptionUser receptionUser) {
      return super.exportXls(request, receptionUser, ReceptionUser.class, "接待人");
  }


	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();


		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(0);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<ReceptionUser> list = ExcelImportUtil.importExcel(file.getInputStream(), ReceptionUser.class, params);
				 if (oConvertUtils.isEmpty(list) || list.size() == 0) return Result.error("文件导入失败: 请维护记录");

				 receptionUserService.saveBatch(list);


				 return Result.ok("文件导入成功！数据行数:");
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.ok("文件导入失败！");
	 }

	 @RequestMapping(value = "/download")
	 public void download(HttpServletRequest req, HttpServletResponse res) {
		 InputStream inputStream = null;
		 OutputStream outputStream = null;
		 try {

			 inputStream = this.getClass().getResourceAsStream("/templates/接待人导入模版.xlsx");
			 outputStream = res.getOutputStream();
			 int temp = 0;
			 while ((temp = inputStream.read()) != -1) {
				 outputStream.write(temp);
			 }
			 res.setContentType("application/force-download");// 设置强制下载不打开
			 res.addHeader("Content-Disposition", "attachment;fileName=" + new String("接待人导入模版.xlsx".getBytes("UTF-8"), "iso-8859-1"));
			 res.setHeader("content-type", "text/html;charset=UTF-8");
			 res.setCharacterEncoding("UTF-8");
		 } catch (Exception e) {
			 log.error("", e);
		 } finally {
			 try {
				 inputStream.close();
			 } catch (IOException e) {
				 e.printStackTrace();
			 }
			 try {
				 outputStream.close();
			 } catch (IOException e) {
				 e.printStackTrace();
			 }
		 }
		 return;
	 }

}
